﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace NewsMine.Storage
{
    public class IndexStoreManagerOnFileSystem : IIndexStoreManager
    {
        public void CreateIndexStore(string name)
        {
            ValidateIndexStoreName(name);

            string indexStoreDirectoryName = GetIndexStoreFolderByName(name);

            if (Directory.Exists(indexStoreDirectoryName))
            {

                //throw new ApplicationException("Already this queuest is exists");
            }

            Directory.CreateDirectory(indexStoreDirectoryName);
        }
        public void DeleteIndexStore(string name)
        {
            ValidateIndexStoreName(name);

            string indexStoreDirectoryName = GetIndexStoreFolderByName(name);

            if (!Directory.Exists(indexStoreDirectoryName))
            {
                throw new ApplicationException("The specified store is not exists on the file system.");
            }

            Directory.Delete(indexStoreDirectoryName, true);
        }
        public string GetIndexStoreFolderByName(string name)
        {
            return Path.Combine(Configuration.ConfigurationManager.IndexesBaseFolder, name);
        }

        private void ValidateIndexStoreName(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException(string.Format("Invalid keyvalue store  name {0}, Please provide the valid key value store name", name));
            }
        }

        public int GetItemCount(string name)
        {
            throw new NotImplementedException();
        }

        public string[] GetListOfIndexStores()
        {
            var subDirs = Directory.GetDirectories(Configuration.ConfigurationManager.IndexesBaseFolder);

            return subDirs;
        }

        public void CreateIndexStore<T>() where T : new()
        {
            string name = (new T()).GetType().Name;

            throw new NotImplementedException();
        }

        public void DeleteIndexStore<T>() where T : new()
        {
            string name = (new T()).GetType().Name;

            throw new NotImplementedException();
        }

        public int GetItemCount<T>() where T : new()
        {
            string name = (new T()).GetType().Name;

            throw new NotImplementedException();
        }

        public IIndexStore<T> GetIndexStore<T>() where T : new()
        {
            string name = (new T()).GetType().Name;

            ValidateIndexStoreName(name);

            string indexStoreDirectoryName = GetIndexStoreFolderByName(name);

            if (!Directory.Exists(indexStoreDirectoryName))
            {
                return null;
            }

            return new IndexStoreOnFileSystem<T>();
        }
    }
}
